import { defineStore } from 'pinia'
import api from '@/services/api/memo'
import { useMemoStore } from '@/stores/memoStore'

export const userInfo = defineStore('userInfo', {
	state: () => ({
		nickName: 'store默认',
		avatarUrl: 'https://pic.616pic.com/ys_bnew_img/00/07/02/JcQoms2o3U.jpg',
		motto: 'store默认'
	}),
	actions: {
		async changeIdentity(avatarUrl, nickName) {
			this.avatarUrl = avatarUrl
			this.nickName = nickName
			await useMemoStore().getUserId()
			const res = await api.changeIdentity(useMemoStore().id, this.avatarUrl, this.nickName)
			console.log('Identity:', res)
		},
		async changeMotto(motto) {
			this.motto = motto
			await useMemoStore().getUserId()
			console.log(this.motto)
			const res = await api.changeMotto(useMemoStore().id, this.motto)
			console.log('Motto:', res)
		},
		
		async initUser() {
			await useMemoStore().getUserId()
			const res = await api.initUser(useMemoStore().id)
			console.log('初始化用户:', res)
		},
		
		async getUserInfo() {
			await useMemoStore().getUserId()
			const { data: res } = await api.getUserInfo(useMemoStore().id)
			console.log('userInfo:', res)
			this.avatarUrl = res.headPhoto
			this.nickName = res.name
			this.motto = res.signature
		}
	}
})